package view;

import javax.swing.ImageIcon;

/**
 * The Chess Image Library will hold all the icons for each button on the chess board
 * This class allow for other custom images to be set as the piece's icon image.
 * If a user would like to add their own image they must create directory with the folders
 * named black and white. They must also name their pieces as either king, queen, knight, pawn,
 * rook, bishop and ghost(if applicable) respectively.
 */
public class ChessImageLibrary {
	public static final String DEFAULT_PATH_DIRECTORY = "images/set1/";
	public static final String SET1_PATH_DIRECTORY = "images/set1/";
	public static final String SET2_PATH_DIRECTORY = "images/set2/";
	public static final String SET3_PATH_DIRECTORY = "images/set3/";
	public static final String SET4_PATH_DIRECTORY = "images/set4/";
	public static final String OPTION_1_PATH_DIRECTORY = "images/";
	public static final String OPTION_2_PATH_DIRECTORY = "images/";
	public static final String WHITE_PLAYER_DIRECTORY = "white/";
	public static final String BLACK_PLAYER_DIRECTORY = "black/";
	
	private static final String BLACK = "Black";
	private static final String WHITE = "White";
	
	private ImageIcon blackPawn;
	private ImageIcon blackBishop;
	private ImageIcon blackKing;
	private ImageIcon blackKnight;
	private ImageIcon blackQueen;
	private ImageIcon blackRook;
	private ImageIcon whitePawn;
	private ImageIcon whiteBishop;
	private ImageIcon whiteKing;
	private ImageIcon whiteKnight;
	private ImageIcon whiteQueen;
	private ImageIcon whiteRook;
	private ImageIcon ghost;
	// Pre-Loaded images for the first set
	private static ImageIcon blackPawn1 = new ImageIcon(SET1_PATH_DIRECTORY + BLACK_PLAYER_DIRECTORY + "pawn.png");
	private static ImageIcon blackBishop1 = new ImageIcon(SET1_PATH_DIRECTORY + BLACK_PLAYER_DIRECTORY + "bishop.png");
	private static ImageIcon blackKing1 = new ImageIcon(SET1_PATH_DIRECTORY + BLACK_PLAYER_DIRECTORY + "king.png");
	private static ImageIcon blackKnight1 = new ImageIcon(SET1_PATH_DIRECTORY + BLACK_PLAYER_DIRECTORY + "knight.png");
	private static ImageIcon blackQueen1 = new ImageIcon(SET1_PATH_DIRECTORY + BLACK_PLAYER_DIRECTORY + "queen.png");
	private static ImageIcon blackRook1 = new ImageIcon(SET1_PATH_DIRECTORY + BLACK_PLAYER_DIRECTORY + "rook.png");
	private static ImageIcon whitePawn1 = new ImageIcon(SET1_PATH_DIRECTORY + WHITE_PLAYER_DIRECTORY + "pawn.png");
	private static ImageIcon whiteBishop1 = new ImageIcon(SET1_PATH_DIRECTORY + WHITE_PLAYER_DIRECTORY + "bishop.png");
	private static ImageIcon whiteKing1 = new ImageIcon(SET1_PATH_DIRECTORY + WHITE_PLAYER_DIRECTORY + "king.png");
	private static ImageIcon whiteKnight1 = new ImageIcon(SET1_PATH_DIRECTORY + WHITE_PLAYER_DIRECTORY + "knight.png");
	private static ImageIcon whiteQueen1 = new ImageIcon(SET1_PATH_DIRECTORY + WHITE_PLAYER_DIRECTORY + "queen.png");
	private static ImageIcon whiteRook1 = new ImageIcon(SET1_PATH_DIRECTORY + WHITE_PLAYER_DIRECTORY + "rook.png");
	private static ImageIcon ghost1 = new ImageIcon(SET1_PATH_DIRECTORY+ "ghost.png");
	
	// Pre-Loaded images for the second set
	private static ImageIcon blackPawn2 = new ImageIcon(SET2_PATH_DIRECTORY + BLACK_PLAYER_DIRECTORY + "pawn.png");
	private static ImageIcon blackBishop2 = new ImageIcon(SET2_PATH_DIRECTORY + BLACK_PLAYER_DIRECTORY + "bishop.png");
	private static ImageIcon blackKing2 = new ImageIcon(SET2_PATH_DIRECTORY + BLACK_PLAYER_DIRECTORY + "king.png");
	private static ImageIcon blackKnight2 = new ImageIcon(SET2_PATH_DIRECTORY + BLACK_PLAYER_DIRECTORY + "knight.png");
	private static ImageIcon blackQueen2 = new ImageIcon(SET2_PATH_DIRECTORY + BLACK_PLAYER_DIRECTORY + "queen.png");
	private static ImageIcon blackRook2 = new ImageIcon(SET2_PATH_DIRECTORY + BLACK_PLAYER_DIRECTORY + "rook.png");
	private static ImageIcon whitePawn2 = new ImageIcon(SET2_PATH_DIRECTORY + WHITE_PLAYER_DIRECTORY + "pawn.png");
	private static ImageIcon whiteBishop2 = new ImageIcon(SET2_PATH_DIRECTORY + WHITE_PLAYER_DIRECTORY + "bishop.png");
	private static ImageIcon whiteKing2 = new ImageIcon(SET2_PATH_DIRECTORY + WHITE_PLAYER_DIRECTORY + "king.png");
	private static ImageIcon whiteKnight2 = new ImageIcon(SET2_PATH_DIRECTORY + WHITE_PLAYER_DIRECTORY + "knight.png");
	private static ImageIcon whiteQueen2 = new ImageIcon(SET2_PATH_DIRECTORY + WHITE_PLAYER_DIRECTORY + "queen.png");
	private static ImageIcon whiteRook2 = new ImageIcon(SET2_PATH_DIRECTORY + WHITE_PLAYER_DIRECTORY + "rook.png");
	
	// Pre-Loaded images for the third set
	private static ImageIcon blackPawn3 = new ImageIcon(SET3_PATH_DIRECTORY + BLACK_PLAYER_DIRECTORY + "pawn.png");
	private static ImageIcon blackBishop3 = new ImageIcon(SET3_PATH_DIRECTORY + BLACK_PLAYER_DIRECTORY + "bishop.png");
	private static ImageIcon blackKing3 = new ImageIcon(SET3_PATH_DIRECTORY + BLACK_PLAYER_DIRECTORY + "king.png");
	private static ImageIcon blackKnight3 = new ImageIcon(SET3_PATH_DIRECTORY + BLACK_PLAYER_DIRECTORY + "knight.png");
	private static ImageIcon blackQueen3 = new ImageIcon(SET3_PATH_DIRECTORY + BLACK_PLAYER_DIRECTORY + "queen.png");
	private static ImageIcon blackRook3 = new ImageIcon(SET3_PATH_DIRECTORY + BLACK_PLAYER_DIRECTORY + "rook.png");
	private static ImageIcon whitePawn3 = new ImageIcon(SET3_PATH_DIRECTORY + WHITE_PLAYER_DIRECTORY + "pawn.png");
	private static ImageIcon whiteBishop3 = new ImageIcon(SET3_PATH_DIRECTORY + WHITE_PLAYER_DIRECTORY + "bishop.png");
	private static ImageIcon whiteKing3 = new ImageIcon(SET3_PATH_DIRECTORY + WHITE_PLAYER_DIRECTORY + "king.png");
	private static ImageIcon whiteKnight3 = new ImageIcon(SET3_PATH_DIRECTORY + WHITE_PLAYER_DIRECTORY + "knight.png");
	private static ImageIcon whiteQueen3 = new ImageIcon(SET3_PATH_DIRECTORY + WHITE_PLAYER_DIRECTORY + "queen.png");
	private static ImageIcon whiteRook3 = new ImageIcon(SET3_PATH_DIRECTORY + WHITE_PLAYER_DIRECTORY + "rook.png");
	// Pre-Loaded images for the fourth set
	private static ImageIcon blackPawn4 = new ImageIcon(SET4_PATH_DIRECTORY + BLACK_PLAYER_DIRECTORY + "pawn.png");
	private static ImageIcon blackBishop4 = new ImageIcon(SET4_PATH_DIRECTORY + BLACK_PLAYER_DIRECTORY + "bishop.png");
	private static ImageIcon blackKing4 = new ImageIcon(SET4_PATH_DIRECTORY + BLACK_PLAYER_DIRECTORY + "king.png");
	private static ImageIcon blackKnight4 = new ImageIcon(SET4_PATH_DIRECTORY + BLACK_PLAYER_DIRECTORY + "knight.png");
	private static ImageIcon blackQueen4 = new ImageIcon(SET4_PATH_DIRECTORY + BLACK_PLAYER_DIRECTORY + "queen.png");
	private static ImageIcon blackRook4 = new ImageIcon(SET4_PATH_DIRECTORY + BLACK_PLAYER_DIRECTORY + "rook.png");
	private static ImageIcon whitePawn4 = new ImageIcon(SET4_PATH_DIRECTORY + WHITE_PLAYER_DIRECTORY + "pawn.png");
	private static ImageIcon whiteBishop4 = new ImageIcon(SET4_PATH_DIRECTORY + WHITE_PLAYER_DIRECTORY + "bishop.png");
	private static ImageIcon whiteKing4 = new ImageIcon(SET4_PATH_DIRECTORY + WHITE_PLAYER_DIRECTORY + "king.png");
	private static ImageIcon whiteKnight4 = new ImageIcon(SET4_PATH_DIRECTORY + WHITE_PLAYER_DIRECTORY + "knight.png");
	private static ImageIcon whiteQueen4 = new ImageIcon(SET4_PATH_DIRECTORY + WHITE_PLAYER_DIRECTORY + "queen.png");
	private static ImageIcon whiteRook4 = new ImageIcon(SET4_PATH_DIRECTORY + WHITE_PLAYER_DIRECTORY + "rook.png");

	
	private String directory;
	
	public ChessImageLibrary(){
		this(DEFAULT_PATH_DIRECTORY);
	}
	
	public ChessImageLibrary(String directory){
		this.directory=directory;
		reloadImages();
	}
	/**
	 * This method will set the directory for specific piece's images
	 * @param directory The directory the files will be in
	 */
	public void setDirectory(String newDirectory){
		this.directory=newDirectory;	
	}
	/**
	 * This will load the images from set 4
	 */
	private void loadImagesSet4(){
		blackPawn = blackPawn4;
		blackBishop= blackBishop4;
		blackKing= blackKing4;
		blackKnight= blackKnight4;
		blackQueen= blackQueen4;
		blackRook= blackRook4;
		whitePawn= whitePawn4;
		whiteBishop= whiteBishop4;
		whiteKing= whiteKing4;
		whiteKnight= whiteKnight4;
		whiteQueen= whiteQueen4;
		whiteRook= whiteRook4;
		ghost	= ghost1;
	}
	/**
	 * This will load the images from set 3
	 */
	private void loadImagesSet3(){
		blackPawn = blackPawn3;
		blackBishop= blackBishop3;
		blackKing= blackKing3;
		blackKnight= blackKnight3;
		blackQueen= blackQueen3;
		blackRook= blackRook3;
		whitePawn= whitePawn3;
		whiteBishop= whiteBishop3;
		whiteKing= whiteKing3;
		whiteKnight= whiteKnight3;
		whiteQueen= whiteQueen3;
		whiteRook= whiteRook3;
		ghost	= ghost1;
	}
	/**
	 * This will load the images from set 2
	 */
	private void loadImagesSet2(){
		blackPawn = blackPawn2;
		blackBishop= blackBishop2;
		blackKing= blackKing2;
		blackKnight= blackKnight2;
		blackQueen= blackQueen2;
		blackRook= blackRook2;
		whitePawn= whitePawn2;
		whiteBishop= whiteBishop2;
		whiteKing= whiteKing2;
		whiteKnight= whiteKnight2;
		whiteQueen= whiteQueen2;
		whiteRook= whiteRook2;
		ghost	= ghost1;
	}
	/**
	 *  This will load the images from set 1
	 */
	private void loadImagesSet1(){
		blackPawn = blackPawn1;
		blackBishop= blackBishop1;
		blackKing= blackKing1;
		blackKnight= blackKnight1;
		blackQueen= blackQueen1;
		blackRook= blackRook1;
		whitePawn= whitePawn1;
		whiteBishop= whiteBishop1;
		whiteKing= whiteKing1;
		whiteKnight= whiteKnight1;
		whiteQueen= whiteQueen1;
		whiteRook= whiteRook1;
		ghost	= ghost1;
	}
	/**
	 * This method will load the image from a custom set
	 */
	private void loadCustomSet(){
		blackPawn = new ImageIcon(directory + BLACK_PLAYER_DIRECTORY + "pawn.png");
		blackBishop= new ImageIcon(directory + BLACK_PLAYER_DIRECTORY + "bishop.png");
		blackKing= new ImageIcon(directory + BLACK_PLAYER_DIRECTORY + "king.png");
		blackKnight= new ImageIcon(directory + BLACK_PLAYER_DIRECTORY + "knight.png");
		blackQueen= new ImageIcon(directory + BLACK_PLAYER_DIRECTORY + "queen.png");
		blackRook= new ImageIcon(directory + BLACK_PLAYER_DIRECTORY + "rook.png");
		whitePawn= new ImageIcon(directory + WHITE_PLAYER_DIRECTORY + "pawn.png");
		whiteBishop= new ImageIcon(directory + WHITE_PLAYER_DIRECTORY + "bishop.png");
		whiteKing= new ImageIcon(directory + WHITE_PLAYER_DIRECTORY + "king.png");
		whiteKnight= new ImageIcon(directory + WHITE_PLAYER_DIRECTORY + "knight.png");
		whiteQueen= new ImageIcon(directory + WHITE_PLAYER_DIRECTORY + "queen.png");
		whiteRook= new ImageIcon(directory + WHITE_PLAYER_DIRECTORY + "rook.png");
		ghost	= new ImageIcon(directory + "ghost.png");
	}
	/**
	 * This method will apply change all the piece images to the selected directory
	 */
	public void reloadImages(){
		if (directory.equals(ChessImageLibrary.SET3_PATH_DIRECTORY)){
			loadImagesSet3();
		}else if(directory.equals(ChessImageLibrary.SET2_PATH_DIRECTORY)){
			loadImagesSet2();
		}else if(directory.equals(ChessImageLibrary.SET1_PATH_DIRECTORY)){
			loadImagesSet1();
		}else if(directory.equals(ChessImageLibrary.SET4_PATH_DIRECTORY)){
			loadImagesSet4();
		}else {
			loadCustomSet();
		}
		
	}
	/**
	 * This method will set the image of a certain button. It check the board to get the
	 * proper piece and sets the correct image to the button
	 * @param button The button that is to be set
	 * @param b The board that is check for piece
	 */
	public void setButtonImage(ChessButton button, chess.Board b){
		chess.Tile curTile=b.getTile(button.getX(), button.getY());
		if(curTile.getPieceHere().getPlayer().getColour().equals(WHITE)){
			setWhitePiece(button,b);
		}else if(curTile.getPieceHere().getPlayer().getColour().equals(BLACK)){
			setBlackPiece(button,b);
		}
	}
	/**
	 * This will clear the selected button's icon to the default button icon
	 * @param curButton The selected button that is to be cleared
	 */
	public void clearTile(ChessButton curButton){
		curButton.getButton().setIcon(null);
	}
	/**
	 * This method will set all the white pieces at a certain button by looking at a 
	 * chess board
	 * @param curButton The current button that is to be set 
	 * @param b The board used as reference for the pieces
	 */
	private void setWhitePiece(ChessButton curButton, chess.Board b){
		chess.Tile curTile=b.getTile(curButton.getX(), curButton.getY());
		if (curTile.getPieceHere() instanceof chess.PawnPiece){
			curButton.getButton().setIcon(whitePawn);
		}
		else if (curTile.getPieceHere() instanceof chess.BishopPiece){
			curButton.getButton().setIcon(whiteBishop);
		}
		else if (curTile.getPieceHere() instanceof chess.KingPiece){
			curButton.getButton().setIcon(whiteKing);
		}
		else if (curTile.getPieceHere() instanceof chess.KnightPiece){
			curButton.getButton().setIcon(whiteKnight);
		}
		else if (curTile.getPieceHere() instanceof chess.QueenPiece){
			curButton.getButton().setIcon(whiteQueen);
		}
		else if (curTile.getPieceHere() instanceof chess.RookPiece){
			curButton.getButton().setIcon(whiteRook);
		}
		//And the ghost
		else if (curTile.getPieceHere() instanceof chess.GhostPiece){
			curButton.getButton().setIcon(ghost);
		}
	}
	/**
	 * This method will set all the black pieces at a certain button by looking at a 
	 * chess board
	 * @param curButton The current button that is to be set 
	 * @param b The board used as reference for the pieces
	 */
	private void setBlackPiece(ChessButton curButton, chess.Board b){
		chess.Tile curTile=b.getTile(curButton.getX(), curButton.getY());
		if (curTile.getPieceHere() instanceof chess.PawnPiece){
			curButton.getButton().setIcon(blackPawn);
		}
		else if (curTile.getPieceHere() instanceof chess.BishopPiece){
			curButton.getButton().setIcon(blackBishop);
		}
		else if (curTile.getPieceHere() instanceof chess.KingPiece){
			curButton.getButton().setIcon(blackKing);
		}
		else if (curTile.getPieceHere() instanceof chess.KnightPiece){
			curButton.getButton().setIcon(blackKnight);
		}
		else if (curTile.getPieceHere() instanceof chess.QueenPiece){
			curButton.getButton().setIcon(blackQueen);
		}
		else if (curTile.getPieceHere() instanceof chess.RookPiece){
			curButton.getButton().setIcon(blackRook);
		}
		
		
		//And the ghost
		else if (curTile.getPieceHere() instanceof chess.GhostPiece){
			curButton.getButton().setIcon(ghost);
		}
	}
}
